import NProgress from 'nprogress'

import router from './router'

import useUserStore from '~stores/module/user'

const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  NProgress.start()

  const { token } = useUserStore()

  if (token) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
